package com.hedonihilist.example.controller;

import com.hedonihilist.example.MyConfig2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/*
    第一种配置方式
    定义类的时候指明前缀
 */
@Component
@ConfigurationProperties(prefix = "config1")
class MyConfig1{
    private String username;
    private String email;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "MyConfig1{" +
                "username='" + username + '\'' +
                ", email='" + email + '\'' +
                '}';
    }
}


@RestController
public class HelloController {

    @Autowired
    private MyConfig1 config1;

    @Autowired
    private MyConfig2 config2;

    @GetMapping("/config1")
    public Map<String,String> get_config1() {
        System.out.println(config1);
        HashMap<String,String> map = new HashMap<>();
        map.put("username", config1.getUsername());
        map.put("email", config1.getEmail());
        return map;
    }

    @GetMapping("/config2")
    public Map<String,String> get_config2() {
        System.out.println(config2);
        HashMap<String,String> map = new HashMap<>();
        map.put("username", config2.getUsername());
        map.put("email", config2.getEmail());
        return map;
    }
}
